package com.engine.jucailinkq.attendance.workflow.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.engine.core.interceptor.CommandContext;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.SubCompanyComInfo;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class GetRestDayIntervalCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetRestDayIntervalCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = Maps.newHashMap();

        String userId = Util.null2String(params.get("userId"));
        String startDate = Util.null2String(params.get("startDate"));
        String endDate = Util.null2String(params.get("endDate"));
        String modeId = Utils.getFormmodeIdMap().get("uf_jcl_kq_rlmc");
        Set<String> calendarSetIdsSets = CommonUtil.getDataIds(userId,modeId, DateUtil.getCurrentDate());
        if (calendarSetIdsSets.size() > 0){
             String sql = "select rqlx,rq from uf_jcl_kq_rlxx where rlmc=? and rq>=? and rq<=?";
            List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,calendarSetIdsSets.toArray()[0],startDate,endDate);
            if (dataList.size() == 0){
                dataList = getDefaultDateType(userId,startDate,endDate);
            }
            resultMap.put("data",dataList);
        }else {
            resultMap.put("data",getDefaultDateType(userId,startDate,endDate));
        }


        return resultMap;
    }

    public List<Map<String,Object>> getDefaultDateType(String userId,String startDate,String endDate){
        List<Map<String,Object>> resultList = Lists.newArrayList();
        String sql = "select subcompanyid1 from hrmresource where id =?";
        Map<String,Object> departMentMap = DbTools.getSqlToMap(sql,userId);
        String subcompanyid1 = departMentMap.get("subcompanyid1").toString();
        String parentSubcompanyids = "";
        try {
            parentSubcompanyids = new SubCompanyComInfo().getAllParentSubcompanyId(subcompanyid1,parentSubcompanyids);
        }catch (Exception e){
            log.error("getDefaultDateType error:[{}]",e);
        }
        parentSubcompanyids = subcompanyid1+parentSubcompanyids;
        sql = "select b.szjg,a.rqlx,a.rq from uf_jcl_kq_rlxx a left join uf_jcl_kq_rlmc b on a.rlmc=b.id where b.mrrl=1 and b.szjg in ("+parentSubcompanyids+") and a.rq>=? and a.rq<=?";
        List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,startDate,endDate);
        Map<String,List<Map<String,Object>>> dataGroupByOrganzation = dataList.stream().collect(Collectors.groupingBy(e->e.get("szjg").toString()));
        for (int i=0;i<parentSubcompanyids.split(",").length;i++){
            String subId = parentSubcompanyids.split(",")[i];
            resultList = dataGroupByOrganzation.get(subId);
            if (resultList != null && resultList.size()>0){
                break;
            }
        }
        return resultList;
    }
}
